﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Collections.Specialized;

namespace MVVMExample;

public partial class ViewModel : ObservableObject
{
    public ViewModel()
    {
        ListOfBoolean.CollectionChanged += (_, _) => OnPropertyChanged(nameof(ListOfBoolean));
        ListOfStudents.CollectionChanged += (_, _) => OnPropertyChanged(nameof(ListOfStudents));
        foreach (Student _student in ListOfStudents)
        {
            _student.PropertyChanged += Student_PropertyChanged;
        }
    }

    private void Student_PropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(Student.IsSelected))
        {
            OnPropertyChanged(nameof(ListOfStudents));
        }
    }

    [ObservableProperty]
    public partial Student Student { get; set; } = new Student { Id = 202401, Name = "张三", Age = 17, IsSelected = false, Gender = GenderType.Men };
    public ObservableCollection<Student> ListOfStudents { get; set; } =
    [
        new Student{ Id=202401, Name="张三", Age=17, IsSelected=false, Gender=GenderType.Men},
        new Student{ Id=202402, Name="李四", Age=18, IsSelected=true, Gender=GenderType.Men},
        new Student{ Id=202403, Name="王五", Age=19, IsSelected=false, Gender=GenderType.Women},
        new Student{ Id=202404, Name="赵六", Age=20, IsSelected=true, Gender=GenderType.Men},
        new Student{ Id=202405, Name="孙七", Age=21, IsSelected=false, Gender=GenderType.Men},
        new Student{ Id=202406, Name="周八", Age=22, IsSelected=true, Gender=GenderType.Women},
        new Student{ Id=202407, Name="吴九", Age=23, IsSelected=false, Gender=GenderType.Men},
    ];

    public ObservableCollection<bool> ListOfBoolean { get; set; } = [true, false];
}

/// <summary>
/// 性别
/// </summary>
public enum GenderType
{
    [Description("男")]
    Men,

    [Description("女")]
    Women
}

public partial class Student : ObservableObject
{
    [ObservableProperty]
    public partial int Id { get; set; }

    [ObservableProperty]
    public partial string Name { get; set; } = string.Empty;

    [ObservableProperty]
    public partial int Age { get; set; }

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Description))]
    public partial bool IsSelected { get; set; }

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Description))]
    public partial GenderType Gender { get; set; }

    public string Description => $"""
        ID: {Id}
        Name: {Name}
        Age: {Age}
        IsSelected: {IsSelected}
        GenderType: {Gender}
        """;

    public override string ToString() => $"""
        ID: {Id}
        Name: {Name}
        Age: {Age}
        IsSelected: {IsSelected}
        GenderType: {Gender}
        """;
}

/// <summary>
/// 多值绑定，所有值为 true 时返回 true，否则返回 false
/// </summary>
public class AllTrueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is IEnumerable<bool> _list )
        {
            return _list.All(_item => _item is true);
        }

        if (value is IEnumerable<Student> _list_of_student)
        {
            return _list_of_student.All(_item => _item.IsSelected is true);
        }
        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}